package com.dotstone.chipism.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dotstone.chipism.R;

public class EditDeviceNameDialog extends Dialog implements
		View.OnClickListener {

	private Button mCancelBtn;
	private Button mEnsureBtn;
	private TextView mDeviceNameTv;
	private EditText mReNameEdit;
	private String mNewName;
	private OnCancelOptionListener onCancelOptionListener;
	private OnEnsureOptionListener onEnsureOptionListener;

	public EditDeviceNameDialog(Context context) {
		
		super(context, R.style.DIYDialog);
		this.setContentView(R.layout.dialog_device_edit);
		this.getWindow().getAttributes().gravity = Gravity.CENTER;
		initView();
		initEvent();
	}

	private void initView() {
		
		mCancelBtn = (Button) findViewById(R.id.cancel_btn);
		mEnsureBtn = (Button) findViewById(R.id.ensure_btn);
		mDeviceNameTv = (TextView) findViewById(R.id.device_name_tv);
		mReNameEdit = (EditText) findViewById(R.id.device_name_edit);
	}

	private void initEvent() {
		
		mCancelBtn.setOnClickListener(this);
		mEnsureBtn.setOnClickListener(this);
	}
	
	public void setDeviceName(String name){
//		mDeviceNameTv.setText(name);
		mReNameEdit.setText(name);
		mReNameEdit.setSelection(name.length());
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.cancel_btn:
			onCancelOptionListener.cancel();
			break;
		case R.id.ensure_btn:
			mNewName = mReNameEdit.getText().toString();
			onEnsureOptionListener.ensure(mNewName);
			break;
		default:
			break;
		}
	}

	public void setOnCancelOptionListener(
			OnCancelOptionListener onCancelOptionListener) {
		this.onCancelOptionListener = onCancelOptionListener;
	}
	
	public void setOnEnsureOptionListener(
			OnEnsureOptionListener onEnsureOptionListener) {
		this.onEnsureOptionListener = onEnsureOptionListener;
	}

	public interface OnEnsureOptionListener {
		public void ensure(String name);
	}

	public interface OnCancelOptionListener {
		public void cancel();
	}
}
